const mongoose = require('mongoose');
const Joi = require('joi');

//创建集合规则
const articleSchema = new mongoose.Schema({
    title: {
        type: String,
        required: [true,'请填写文章标题'],
        minlength: 4,
        maxlength: 20
    },
    author: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User', //与User 集合关联
        required: [true,'请填写作者'] 
    },
    publishDate: {
        type: Date,
        default: Date.now
    },
    cover: {
        type: String,
        default: null
    },
    content: {
        type: String
    }
});

//创建集合
const Article = mongoose.model('Article',articleSchema);

// Article.create({
//     articlename: 'saf',
// });

const validateArticle = (article) => {
    const Schema = {
        title: Joi.string().minlength(2).maxlength(20).required().error(new Error('')),
    }

    return Joi.validate(article,Schema)
}

module.exports = {
    Article,
    validateArticle
}